# autoplot.R
# draws the stuff

MIN_ERRO <- .015

plotfit <- function(x, ydados, distrs)
{
	ydistr <- gendists(distrs, x)

	# desenhar dados
	plot(x=NULL, y=NULL,
		main=paste('error:', round(calcerror(ydados, ydistr), 6)),
		type='l', col='lightgray', lwd=1,
		xlab='Damage', ylab='Frequency', cex.lab=0.8,
		xlim=c(x[1], x[length(x)]), ylim=c(0, max(ydados)))

	w <- x[2]-x[1]
	for(i in 1:length(x)) {
		rect(x[i]-w/2, 0, x[i]+w/2, ydados[i], border='gray', lwd=1)
		#lines(c(x[i]-w/2, x[i]+w/2), c(ydados[i], ydados[i]),
		#	col='gray', lwd=1)
		#if(i < length(x))
		#	lines(c(x[i+1]-w/2, x[i+1]-w/2), c(ydados[i], ydados[i+1]),
		#		col='gray', lwd=1)
	}

	# desenhar distribuicoes
	x. <- seq(x[1], x[length(x)], length=1000)
	for(d in distrs) {
		y <- gendist(x., d)
		lines(x., y, col='violet', lty=2, lwd=1)
	}
	lines(x., gendists(distrs, x.), col='violet', lwd=2)

	# desenhar pontos dos dados / pontos de controlo
	match <- abs(ydados - ydistr) < MIN_ERRO
	points(x[match], ydados[match], pch=21, bg='black', col='black', lwd=2)
	points(x[!match], ydados[!match], pch=21, bg='white', col='black', lwd=2)
}

